//
//  KATPopWindow.m
//  KATFramework
//
//  Created by Kantice on 14-8-28.
//  Copyright (c) 2014年 KatApp. All rights reserved.
//

#import "KATPopWindow.h"



@interface KATPopWindow ()

//内容
@property(nonatomic,retain) KATSprite *content;

//是否已经显示
@property(nonatomic,assign) BOOL isShown;

//模糊背景视图
@property(nonatomic,retain) UIVisualEffectView *blurView;

//模糊动画视图（用于模糊进出动画）
@property(nonatomic,retain) UIVisualEffectView *blurAnimationView;

//模糊动画效果（用于模糊进出动画）
@property(nonatomic,retain) UIVisualEffect *blurEffect;

//屏幕宽度(用于检测屏幕是否发生旋转)
@property(nonatomic,assign) double screenWidth;


@end


@implementation KATPopWindow


+ (instancetype)popWindow
{
    KATPopWindow *window=[[[self alloc] init] autorelease];
    
    [window initData];
    
    return window;
}


//初始化数据
- (void)initData
{
    self.screenSize=[[UIScreen mainScreen] bounds].size;
    self.screenWidth=self.screenSize.width;
    self.frame=CGRectMake(_screenSize.width, 0, _screenSize.width, _screenSize.height);//开始隐藏
    self.backgroundColor=[UIColor clearColor];
    self.cornerRadius=8.0f;
    self.bgColor=COLOR(COLOR_WHITE-5);
    self.shadowColor=KCOLOR_SHADOW;
    self.showDuration=0.3f;
    self.hideDuration=0.3f;
    self.placement=CGPointMake(_screenSize.width*0.5, _screenSize.height*0.5);
    self.contentSize=CGSizeMake(_screenSize.width*0.72, _screenSize.width*0.45);
    self.isTapShadowToHide=NO;
    self.isTapContentToHide=NO;
    self.isBlurStyle=NO;
    self.blurAnimationView=nil;
    self.blurEffect=nil;
    self.windowDelegate=nil;
    
    self.showMode=KATPopWindowAnimationTypeMovingRight;//默认显示模式
    self.hideMode=KATPopWindowAnimationTypeMovingLeft;//默认隐藏模式
}


//初始化
- (void)initWindow
{
    self.shadow=[[[UIView alloc] init] autorelease];
    _shadow.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    _shadow.backgroundColor=_shadowColor;
    [self addSubview:_shadow];
    
    self.content=[KATSprite sprite];
    _content.bounds=CGRectMake(0, 0, _contentSize.width, _contentSize.height);
    _content.center=_placement;
    _content.backgroundColor=_bgColor;
    _content.layer.cornerRadius=_cornerRadius;
    _content.layer.masksToBounds=YES;
    [self addSubview:_content];
    
    self.blurView=[[[UIVisualEffectView alloc] init] autorelease];
    _blurView.effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    _blurView.layer.cornerRadius=_content.layer.cornerRadius;
    _blurView.frame=_content.bounds;
    _blurView.hidden=YES;
    [_content addSubview:_blurView];
    
    if(_isBlurStyle)
    {
        _blurView.hidden=NO;
        _content.backgroundColor=[UIColor clearColor];
    }
    
    
    _isShown=NO;
    
    //点击事件
    //按钮点击事件
    UITapGestureRecognizer *singleTap=nil;
    
    singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentTaped)];
    singleTap.numberOfTapsRequired=1;//点击次数
    [_content addGestureRecognizer:singleTap];//添加点击事件
    [singleTap release];
    singleTap=nil;
    
    singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shadowTaped)];
    singleTap.numberOfTapsRequired=1;//点击次数
    [_shadow addGestureRecognizer:singleTap];//添加点击事件
    [singleTap release];
    singleTap=nil;
    
    
    //注册屏幕旋转事件通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenRotary) name:UIDeviceOrientationDidChangeNotification object:nil];
}


- (void)setIsBlurStyle:(BOOL)isBlurStyle
{
    _isBlurStyle=isBlurStyle;
    
    if(_blurView)
    {
        if(_isBlurStyle)
        {
            _blurView.hidden=NO;
            _content.backgroundColor=[UIColor clearColor];
        }
        else
        {
            _blurView.hidden=YES;
            _content.backgroundColor=_bgColor;
        }
    }
}


- (void)setContentSize:(CGSize)contentSize
{
    _contentSize=contentSize;
    
    if(_content)
    {
        _content.bounds=CGRectMake(0, 0, contentSize.width, contentSize.height);
        
        if(_blurView)
        {
            _blurView.frame=_content.bounds;
        }
        
        //回调
        if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowDidResize:)])
        {
            [_windowDelegate windowDidResize:self];
        }
    }
}



//调整窗口(方向改变系统通知处理)
- (void)resizeWindow
{
    self.frame=RECT(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _shadow.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    _placement=CGPointMake(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.5);
    _content.center=_placement;
}


//屏幕旋转
- (void)screenRotary
{
    if(fabs(_screenWidth-SCREEN_WIDTH)>1.0)
    {
        _screenWidth=SCREEN_WIDTH;
        
        [self resizeWindow];
    }
}


//显示
- (void)show
{
    if(!_isShown)
    {
        [[KATAppUtil keyWindow] addSubview:self];
        
        //清除动画
        [_content clearAnimations];
                
        if(_showDuration<=0.05 || _showMode==KATPopWindowAnimationTypeNone)//无动画效果
        {
            if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowWillShow:)])
            {
                [_windowDelegate windowWillShow:self];
            }
            
            _isShown=YES;
            
            self.frame=CGRectMake(0, 0, _screenSize.width, _screenSize.height);
            _shadow.alpha=1.0;
            
            if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowDidShow:)])
            {
                [_windowDelegate windowDidShow:self];
            }
        }
        else
        {
            if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowWillShow:)])
            {
                [_windowDelegate windowWillShow:self];
            }
            
            //恢复原型
            [_content restoreSprite];
            
            //动画前窗口的位置及速度
            if(_showMode==KATPopWindowAnimationTypeMovingRight)//右移
            {
                _content.center=CGPointMake(self.bounds.size.width+_contentSize.width*0.5, _placement.y);
                
                [_content moveToPosition:_placement withDuration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeMovingLeft)//左移
            {
                _content.center=CGPointMake(-_contentSize.width*0.5, _placement.y);
                
                [_content moveToPosition:_placement withDuration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeMovingUp)//上移
            {
                _content.center=CGPointMake(_placement.x, -_contentSize.height*0.5);
                
                [_content moveToPosition:_placement withDuration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeMovingDown)//下移
            {
                _content.center=CGPointMake(_placement.x, self.bounds.size.height+_contentSize.height*0.5);
                
                [_content moveToPosition:_placement withDuration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeRunningRight)//右闪
            {
                _content.center=CGPointMake(self.bounds.size.width+_contentSize.width*0.5, _placement.y);
                
                [_content moveQuicklyToPosition:_placement withDegree:-1 duration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeRunningLeft)//左闪
            {
                _content.center=CGPointMake(-_contentSize.width*0.5, _placement.y);
                
                [_content moveQuicklyToPosition:_placement withDegree:-1 duration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeRunningUp)//上闪
            {
                _content.center=CGPointMake(_placement.x, -_contentSize.height*0.5);
                
                [_content moveQuicklyToPosition:_placement withDegree:-1 duration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeRunningDown)//下闪
            {
                _content.center=CGPointMake(_placement.x, self.bounds.size.height+_contentSize.height*0.5);
                
                [_content moveQuicklyToPosition:_placement withDegree:-1 duration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeAlpha)//透明
            {
                _content.center=_placement;
                _content.alpha=0.0f;
                
                [_content appearWithDuration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeScale)//缩放
            {
                _content.center=_placement;
                
                [_content transformScaleToWitdh:0.05 andHeight:0.05];
                [_content scaledAppearWithDuration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeZoom)//对焦
            {
                _content.center=_placement;
                
                [_content zoomInWithScale:1.28 duration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeRotate)//旋转
            {
                _content.center=_placement;
                
                [_content rotateInWithCircles:-1 duration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeDrop)//掉落
            {
                _content.center=CGPointMake(_placement.x, -_contentSize.height*0.5);
                
                [_content dropToPosition:_placement withDuration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeFloat)//飘落
            {
                _content.center=CGPointMake(_placement.x, -_contentSize.height*0.5);
                
                [_content floatToPosition:_placement withDuration:_showDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeBlur || _showMode==KATPopWindowAnimationTypeBlurScale)//模糊
            {
                //初始化模糊效果
                
                if(!_blurAnimationView)
                {
                    self.blurAnimationView=[[[UIVisualEffectView alloc] init] autorelease];
                }
                
                if(!_blurEffect)
                {
                    self.blurEffect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
                }
                
                _blurAnimationView.frame=CGRectMake(0, 0, _content.bounds.size.width, _content.bounds.size.height);
                [_blurAnimationView removeFromSuperview];
                
                //动画初始化
                [_content addSubview:_blurAnimationView];
                _blurAnimationView.effect=_blurEffect;
                
                _content.center=_placement;
                
                //缩放动画
                if(_showMode==KATPopWindowAnimationTypeBlurScale)
                {
                    [_content transformScaleToWitdh:0.05 andHeight:0.05];
                    [_content scaledAppearWithDuration:_showDuration];
                }
                
                //模糊动画
                [UIView animateWithDuration:_showDuration animations:^
                {
                    _blurAnimationView.effect=nil;
                }
                completion:^(BOOL finished)
                {
                    [_blurAnimationView removeFromSuperview];
                }];
            }

            
            self.frame=CGRectMake(0, 0, _screenSize.width, _screenSize.height);
            _shadow.alpha=0.0;
            _shadow.backgroundColor=_shadowColor;
            
            //阴影变化
            [UIView animateWithDuration:_showDuration animations:^
            {
                _shadow.alpha=1.0;
            }
            completion:^(BOOL finished)
            {
                _isShown=YES;
                
                if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowDidShow:)])
                {
                    [_windowDelegate windowDidShow:self];
                }
            }];
        }
    }
}


//隐藏
- (void)hide
{
    if(_isShown)
    {
        [_content clearAnimations];
        
        if(_hideDuration<=0.05 || _hideMode==KATPopWindowAnimationTypeNone)//无动画效果
        {
            if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowWillHide:)])
            {
                [_windowDelegate windowWillHide:self];
            }
            
            _isShown=NO;
            
            self.frame=CGRectMake(-_screenSize.width, 0, _screenSize.width, _screenSize.height);
            _shadow.alpha=0.0;
                        
            if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowDidHide:)])
            {
                [_windowDelegate windowDidHide:self];
            }
        }
        else
        {
            if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowWillHide:)])
            {
                [_windowDelegate windowWillHide:self];
            }
            
            if(_hideMode==KATPopWindowAnimationTypeMovingRight)//右移
            {
                [_content moveToPosition:CGPointMake(self.bounds.size.width+_contentSize.width*0.5, _placement.y) withDuration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeMovingLeft)//左移
            {                
                [_content moveToPosition:CGPointMake(-_contentSize.width*0.5, _placement.y) withDuration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeMovingUp)//上移
            {
                [_content moveToPosition:CGPointMake(_placement.x, -_contentSize.height*0.5) withDuration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeMovingDown)//下移
            {
                [_content moveToPosition:CGPointMake(_placement.x, self.bounds.size.height+_contentSize.height*0.5) withDuration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeRunningRight)//右闪
            {
                [_content moveQuicklyToPosition:CGPointMake(self.bounds.size.width+_contentSize.width*0.5, _placement.y) withDegree:-1 duration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeRunningLeft)//左闪
            {
                [_content moveQuicklyToPosition:CGPointMake(-_contentSize.width*0.5, _placement.y) withDegree:-1 duration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeRunningUp)//上闪
            {
                [_content moveQuicklyToPosition:CGPointMake(_placement.x, -_contentSize.height*0.5) withDegree:-1 duration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeRunningDown)//下闪
            {
                [_content moveQuicklyToPosition:CGPointMake(_placement.x, self.bounds.size.height+_contentSize.height*0.5) withDegree:-1 duration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeAlpha)//透明
            {
                [_content disappearWithDuration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeScale)//缩放
            {
                [_content scaledDisappearWithDuration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeZoom)//对焦
            {
                [_content zoomOutWithScale:1.28 duration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeRotate)//旋转
            {
                [_content rotateOutWithCircles:-1 duration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeDrop)//掉落
            {
                [_content dropToPosition:CGPointMake(_placement.x, self.bounds.size.height+_contentSize.height*0.5) withDuration:_hideDuration];
            }
            else if(_hideMode==KATPopWindowAnimationTypeFloat)//飘落
            {
                [_content floatToPosition:CGPointMake(_placement.x, self.bounds.size.height+_contentSize.height*0.5) withDuration:_hideDuration];
            }
            else if(_showMode==KATPopWindowAnimationTypeBlur || _showMode==KATPopWindowAnimationTypeBlurScale)//模糊
            {
                //初始化模糊效果
                
                if(!_blurAnimationView)
                {
                    self.blurAnimationView=[[[UIVisualEffectView alloc] init] autorelease];
                }
                
                if(!_blurEffect)
                {
                    self.blurEffect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
                }
                
                _blurAnimationView.frame=CGRectMake(0, 0, _content.bounds.size.width, _content.bounds.size.height);
                [_blurAnimationView removeFromSuperview];
                
                //动画初始化
                [_content addSubview:_blurAnimationView];
                _blurAnimationView.effect=nil;
                
                //缩放动画
                if(_showMode==KATPopWindowAnimationTypeBlurScale)
                {
                    [_content scaledDisappearWithDuration:_hideDuration];
                }
                
                //模糊动画
                [UIView animateWithDuration:_showDuration animations:^
                {
                    _blurAnimationView.effect=_blurEffect;
                }
                completion:^(BOOL finished)
                {
                    [_blurAnimationView removeFromSuperview];
                }];
            }
            
            
            [UIView animateWithDuration:_hideDuration animations:^
            {
                _shadow.alpha=0.0;

            }
            completion:^(BOOL finished)
            {
                _isShown=NO;
                
                self.frame=CGRectMake(_screenSize.width, 0, _screenSize.width, _screenSize.height);
                _content.center=_placement;
                [_content restoreSprite];
                
                [self removeFromSuperview];
                
                if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowDidHide:)])
                {
                    [_windowDelegate windowDidHide:self];
                }
            }];
            
        }
        
    }

}




//抖动
- (void)shake
{
    if(_isShown)
    {
        [_content clearAnimations];
        
        [_content shakeWithDegree:2.0 duration:-1 repeat:4];
    }
}

//内容点击
- (void)contentTaped
{
    if(_isTapContentToHide)
    {
        [self hide];
    }
    
    if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowContentTaped:)])
    {
        [_windowDelegate windowContentTaped:self];
    }
}


//阴影点击
- (void)shadowTaped
{
    if(_isTapShadowToHide)
    {
        [self hide];
    }
    
    if(_windowDelegate && [_windowDelegate respondsToSelector:@selector(windowShadowTaped:)])
    {
        [_windowDelegate windowShadowTaped:self];
    }
}



- (void) dealloc
{
    //取消通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [_shadow release];
    [_content release];
    [_bgColor release];
    [_shadowColor release];
    [_blurView release];
    [_blurAnimationView release];
    [_blurEffect release];
    
    [super dealloc];
}


@end
